package com.pronhub.luowowo.member.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.pronhub.luowowo.excption.LoginException;
import com.pronhub.luowowo.member.domain.UserInfo;
import com.pronhub.luowowo.member.mapper.UserInfoMapper;
import com.pronhub.luowowo.member.service.IUserInfoService;
import com.pronhub.luowowo.service.IUserInfoRedisService;
import com.pronhub.luowowo.util.CheckTheDate;
import com.pronhub.luowowo.util.Consts;
import com.pronhub.luowowo.util.Rediskey;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public UserInfo selectByID(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        int i = userInfoMapper.selectByPhone(phone);
        return i>0;
    }

    @Reference
    private IUserInfoRedisService infoRedisService;
    @Override
    public void sendVerifyCode(String phone) {

        String key = Rediskey.VERIFY_CODE.getKey(phone);
        System.err.println(key);
        //短信发送的频率校验
        String codeByKey = infoRedisService.getCodeByKey(key);
        if (codeByKey != null){
            throw new RuntimeException("发送短信太平凡");
        }
        //1 生成验证码
        //获取一个四位数的验证码
        String code = String.valueOf((int)((Math.random()*9+1)*1000));

        //2 放到一个可以大家都能快速用到的地方 为什么选用Redis?
        infoRedisService.saveVerifCode(key,code,Consts.VERIFY_CODE_VAI_TIME);

        //3 短信的内容
        String msgCode = "天上人间会所验证码是:[" +code+"]在"+ Consts.VERIFY_CODE_VAI_TIME+"分钟内进行登录有效.";
        //真实发短信的代码
        System.err.println(msgCode);
        //发送验证码
       /*RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(
                "http://utf8.api.smschinese.cn/?Uid=lxyccr1&Key=d41d8cd98f00b204e980="+phone+"&smsText="+sb.toString(), String.class);
        System.out.println(result);
        */
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode)
        throws LoginException
    {
        //1 对所有的数据进行相对应的校验工作  自己来一个工具类
        CheckTheDate.checkTheData(phone,"电话号码不能为空");
        CheckTheDate.checkTheData(nickname,"昵称不能为空");
        CheckTheDate.checkTheData(password,"密码不能为空");
        CheckTheDate.checkTheData(rpassword,"第二次密码不能为空");
        CheckTheDate.checkTheData(verifyCode,"验证码不能为空");

        //2 校验 这两个密码是否一致
        CheckTheDate.checkThePassword(password,rpassword,"两次密码不一致,请重新输入");

        //3 判断手机号码是否符合要求 正则表达式
        boolean phone1 = CheckTheDate.isPhone(phone);
        if (!phone1){
            throw  new LoginException("手机格式不正确");
        }
        //4 判断是否存在相对应的账号
        int i = userInfoMapper.selectByPhone(phone);
        if (i>0){
            throw  new LoginException("账号已被注册");
        }
        //5判断验证码是否正确 是否超时
        String key = "verify_code:"+phone;  //是Redis的Key
        String code = infoRedisService.getCodeByKey(key);

        CheckTheDate.checkTheCode(code,verifyCode,"验证码错误");

        //6 给用户注册
        UserInfo user = new UserInfo();
        user.setNickname(nickname);
        user.setPassword(password);
        user.setPhone(phone);
        user.setLevel(1);
        user.setState(UserInfo.STATE_NORMAL);
        user.setHeadImgUrl("/images/default.jsp");
        userInfoMapper.insert(user);

    }

    @Override
    public String userLoginByusernameAndPassword(String username, String password)throws LoginException {
        UserInfo user = userInfoMapper.selectByUsernameAndPassword(username,password);
        //得到数据后 生产出来一串唯一的ID 然后user对象也转变成String存到Redis里面
        if (user == null){
            throw new LoginException("账号密码错误");
        }
        //1 user 的数据
        String userStr = JSON.toJSONString(user);
        // 唯一的钥匙窜数据
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //存到相关来的Redis中
        String token = Rediskey.USER_LOGIN.getKeys(username, uuid);
        System.err.println(token);
        infoRedisService.saveUserInfoByToken(token,userStr,Consts.USER_INFO_TOKEN_VAI_TIME);
        return token;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }
}
